﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using Common;
using System.Data;
using System.Collections;

public partial class product_list : MemberPageBase
{
    public string PaginationBar = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        if (!IsPostBack)
        {
            BrandTypeDataBind();
            CategoryDataBind(); 

            RedProductList();
           
        }

        ProductListDataBind();
    }

    #region 查询选项
    void BrandTypeDataBind()
    {
        var bindControl = new BindControl();
        bindControl.BindEnumItems_HtmlSelect(selRange, "r_integralNumber", true, "全部积分范围");
    } 
    void CategoryDataBind()
    {
        DataTable dt = BLL.CommonBLL.Select("ProductCategoryBase", "*", " AND DeletionStateCode = 0 ORDER BY LineNum ASC");
        DropdownlistDataBind(1, "00000000-0000-0000-0000-000000000000", dt);
    } 
    /// <summary>
    /// 类别列表邦定
    /// </summary>
    /// <param name="intLayer">级别</param>
    /// <param name="parentId">父目录ID</param>
    /// <param name="dt">目录数据表</param>
    void DropdownlistDataBind(int intLayer, string parentId, DataTable dt)
    {
        if (dt == null)
            return;

        DataRow[] dr = dt.Select("Layer = " + intLayer.ToString() + " AND ParentId = '" + parentId + "'");

        ArrayList al = new ArrayList();
        ListItem li;

        string str = ""; int intCategoryLayerID = 0;
        foreach (DataRow d in dr)
        {
            str = "　| ";
            intCategoryLayerID = int.Parse(d["Layer"].ToString());
            if (intLayer == 1)
                str = " + ";
            for (int i = 1; i < intLayer * intLayer; i++)
                str += "--";

            li = new ListItem("\t" + str + "\t" + d["CategoryName"].ToString(), d["CategoryNumber"].ToString());
            selCategoryList.Items.Add(li);
            DropdownlistDataBind(intCategoryLayerID + 1, d["CategoryId"].ToString().Trim(), dt);
        }
    } 
    #endregion

    #region 信息绑定 
    void ProductListDataBind()
    {
        string urlStr = "product_list.aspx?";

        StringBuilder SQL = new StringBuilder();
        SQL.Append(" AND DeletionStateCode = 0 ");
        if (RequestBase.GetQueryString("qtype") != "" && Utils.IsNumber(RequestBase.GetQueryString("categorynumber")))
        {
            SQL.Append(" AND CategoryNumber LIKE '" + RequestBase.GetQueryString("categorynumber") + "%'");
        }
         
        if (!string.IsNullOrEmpty(RequestBase.GetFormString("selRange")))
        {
            var range = RequestBase.GetFormString("selRange").Split('_');
            SQL.Append(" AND (price BETWEEN  " + range[1] + " AND " + range[2] + ")"); 
        }
        if (this.cbxIsCan.Checked)
        {
            SQL.Append(" AND price <= " + UserInfo.UserIntegral.ToString());  
        } 
        if (!string.IsNullOrEmpty(RequestBase.GetFormString("selCategoryList")))
        {
            SQL.Append(" AND CategoryNumber LIKE '" + RequestBase.GetFormString("selCategoryList") + "%'");  
        }

        #region 分页
        DBUtility.Pagination pagination = new DBUtility.Pagination();
        /// 页码
        if (RequestBase.GetString("page") != "" && Utils.IsNumber(RequestBase.GetString("page")))
            pagination.Sequence = Convert.ToInt32(RequestBase.GetString("page"));
        else
            pagination.Sequence = 1;

        /// 每页显示记录数
        int pagesize = 12;
        if (RequestBase.GetString("pageSize") != "" && Utils.IsNumber(RequestBase.GetString("pageSize")))
            pagesize = int.Parse(RequestBase.GetString("pageSize"));

        /// 排序方式
        string sortType = " ORDER BY Price2 asc , ModifiedOn desc ";
        if (RequestBase.GetString("pageSize") != "" && Utils.IsNumber(RequestBase.GetString("pageSize")))
        {
            switch (RequestBase.GetString("pageSize"))
            {
                case "30":
                    sortType = " ORDER BY Price ASC";
                    break;
                case "40":
                    sortType = " ORDER BY Price DESC";
                    break;
                default:
                    sortType = "ORDER BY Price2 asc , ModifiedOn desc";
                    break;
            }
        }

        if (RequestBase.GetString("upcount") != "" && Utils.IsNumber(RequestBase.GetString("upcount")))
            pagination.PageUpCount = Convert.ToInt32(RequestBase.GetString("upcount"));

        if (RequestBase.GetString("direction") == "next")
            pagination.Direction = "next";
        else
            pagination.Direction = "prev";

        pagination.PageBarCount = 8;
        pagination.UrlStr = urlStr;
        pagination.PageSize = pagesize;
        pagination.Primarykey = "ProductId";
        pagination.SearchField = "*";
        pagination.TableName = "vw_ProductBase";
        pagination.ItemUnit = "件商品";
        pagination.SearchSentence = SQL.ToString();
        pagination.SortSentence = sortType;

        rptProductList.DataSource = pagination.GetDataTable();
        rptProductList.DataBind();

        pagination.GoNumeberPageBarCn2();
        PaginationBar = pagination.PageBar;
        #endregion
    } 
    #endregion
     
    #region 特惠商品
    void RedProductList()
    {
        DataTable dat = BLL.CommonBLL.Select("ProductBase", "TOP 8 *", " AND DeletionStateCode = 0  ORDER BY ProductTypeCode desc");
        rptRecProductList.DataSource = dat;
        rptRecProductList.DataBind();
    }
    #endregion
}